import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@WebServlet(name = "TestSerciet",urlPatterns = {"/get"})

public class TestSerciet extends HttpServlet {
    static Map<Integer,String> map =new HashMap<>();
    static{
        map.put(1,"aa");
        map.put(2,"bb");
        map.put(3,"cc");
        map.put(4,"dd");
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("get");//get会在地址栏上显示你的请求信息(你输入的信息会放在地址栏上)，数据量小
        String id = req.getParameter("id");
        //给客户端返回数据
        PrintWriter out = resp.getWriter();
        /*if(id!=null){
            for(Integer key:map.keySet()){
                if(key==Integer.parseInt(id)){
                    out.write(map.get(key));
                    return;
                }else{
                    out.write("No such id");
                    return;
                }
            }
        }else{
            out.write("No such id");
        }*/
        if(id==null||Integer.parseInt(id)>4||Integer.parseInt(id)<0){
            String name = "No such id";
            out.write(name);
        }else {
            int id1 =Integer.parseInt(id);
            String name=map.get(id1);
            out.write(name);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("post");//post不会在地址栏上显示，数据量大
    }
}
